﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel.Composition;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UCL.Powersource.ViewModels.Analysis;
using UCL.Powersource;
using PowersourceUIToolkit;
using UCL.Powersource.Analysis;

namespace UCL.Powersource
{
    public class ReportingManager : InitializedLifeCycleBase, IReportingManager
    {
        [ImportMany(typeof(IStatisticViewModel))]
        private List<IStatisticViewModel> _StatisticViewModels;

        protected override bool OnInitializing()
        {
            if (_StatisticViewModels == null)
            {
                var r = IoC.GetAllInstances(typeof(IStatisticViewModel));
                _StatisticViewModels = r.Cast<IStatisticViewModel>().ToList();
            }
            Log.Debug("Imported IStatisticViewModel:");
            if (_StatisticViewModels != null && _StatisticViewModels.Count > 0)
            {
                foreach (var it in _StatisticViewModels)
                {
                    Log.Debug("{0} from {1}", it.GetType().FullName, it.GetType().AssemblyQualifiedName);
                }
                StatisticViewModels = new System.Collections.ObjectModel.ReadOnlyCollection<IStatisticViewModel>(_StatisticViewModels);
            }
            else
            {
                Log.Warn("No statistic view found! Reporting will be empty!");
                StatisticViewModels = new System.Collections.ObjectModel.ReadOnlyCollection<IStatisticViewModel>(new List<IStatisticViewModel>());
            }
            return true;
        }

        protected override void OnDisposing()
        {
        }

        public System.Collections.ObjectModel.ReadOnlyCollection<IStatisticViewModel> StatisticViewModels
        {
            get;
            private set;
        }

        public void RegisterStatistics()
        {
            foreach (var it in StatisticViewModels)
            {
                RegisterStatistics(it);
            }
        }

        public void RegisterStatistics(IStatisticViewModel it)
        {
            var ipowersourceext = PowersourceConnector.GetExtensibilityPoint();
            var statman = ipowersourceext.StatisticManager;
            foreach (var stat in statman.AvailableStatistics.Values)
            {
                if (!_ViewModelRegisteredStatistics.ContainsKey(it))
                {
                    _ViewModelRegisteredStatistics.Add(it, new List<IStatistic>());
                }
                if (it.TryRegisterStatistic(stat))
                {
                    if (!_ViewModelRegisteredStatistics[it].Contains(stat))
                    {
                        _ViewModelRegisteredStatistics[it].Add(stat);
                    }
                }
                else
                {
                    if (_ViewModelRegisteredStatistics[it].Contains(stat))
                    {
                        _ViewModelRegisteredStatistics[it].Remove(stat);
                    }
                }
            }
        }
        private Dictionary<IStatisticViewModel, List<IStatistic>> _ViewModelRegisteredStatistics=new Dictionary<IStatisticViewModel,List<IStatistic>>();
        
        public IEnumerable<IStatistic> GetRegisteredStatistics(IStatisticViewModel ivm)
        {
            if (ivm == null) throw new ArgumentNullException("ivm");
            if (_ViewModelRegisteredStatistics == null) _ViewModelRegisteredStatistics=new Dictionary<IStatisticViewModel,List<IStatistic>>();
            if (!_ViewModelRegisteredStatistics.ContainsKey(ivm))
            {
                RegisterStatistics(ivm);
                //_ViewModelRegisteredStatistics.Add(ivm, new List<IStatistic>());
            }
            return new ReadOnlyCollection<IStatistic>(_ViewModelRegisteredStatistics[ivm]);
        }


        private List<IReportingContextStrategy> _IReportingContextStrategiesList;
        private ReadOnlyCollection<IReportingContextStrategy> _IReportingContextStrategies;
        public ReadOnlyCollection<IReportingContextStrategy> ReportingContextStrategies
        {
            get 
            {
                if (_IReportingContextStrategiesList == null)
                {
                    _IReportingContextStrategiesList  = new List<IReportingContextStrategy>(IoC.GetAllInstances(typeof(IReportingContextStrategy)).Cast<IReportingContextStrategy>());
                    _IReportingContextStrategies = new ReadOnlyCollection<IReportingContextStrategy>(_IReportingContextStrategiesList);
                }
                if (_IReportingContextStrategies == null)
                {
                    _IReportingContextStrategies = new ReadOnlyCollection<IReportingContextStrategy>(_IReportingContextStrategiesList);
                }
                return _IReportingContextStrategies;
            
            }
        }
    }

}
